package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmNewsDto;
import com.heima.media.dto.WmNewsPageDto;
import com.heima.media.entity.WmNews;
import com.heima.media.mapper.WmNewsMapper;
import com.heima.media.service.IWmNewsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 自媒体图文内容信息表   服务实现类
 */
@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews>implements IWmNewsService {

    @Override
    public ResponseResult listByCondition(WmNewsPageDto dto) {
        // 判断是否有登录人
        User user = UserThreadLocalUtil.get();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 做各种条件的查询
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null){
            queryWrapper.eq(WmNews::getStatus,dto.getStatus());
        }
        if (dto.getChannelId() != null){
            queryWrapper.eq(WmNews::getChannelId,dto.getChannelId());
        }
        if (dto.getBeginPubDate() != null && dto.getEndPubDate() != null){
//            WmNews::getPublishTime 大于等于 getBeginPubDate
//            WmNews::getPublishTime 小于等于 getEndPubDate
            queryWrapper.ge(WmNews::getPublishTime,dto.getBeginPubDate());
            queryWrapper.le(WmNews::getPublishTime,dto.getEndPubDate());
        }
        if (StringUtils.isNotBlank(dto.getKeyword())){
            queryWrapper.like(WmNews::getTitle,dto.getKeyword());
        }
        queryWrapper.eq(WmNews::getUserId,user.getUserId());
//        隐藏条件：登录人
        queryWrapper.orderByDesc(WmNews::getPublishTime);
//        自媒体文章发布时间  倒序  顺序
        Page<WmNews> page = new Page<>(dto.getPage(), dto.getSize());

        Page<WmNews> page1 = this.page(page,queryWrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }

    @Override
    public ResponseResult submit(WmNewsDto wmNewsDto) {
        return null;
    }
}
